package com.launcher.wkang.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Browser;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.launcher.wkang.App;
import com.launcher.wkang.R;
import com.launcher.wkang.activity.LauncherActivity;
import com.launcher.wkang.adapter.AfterTimeAdapter;
import com.launcher.wkang.adapter.CourseTimeAdapter;
import com.launcher.wkang.adapter.MorningTimeAdapter;
import com.launcher.wkang.bean.CouListBean;
import com.launcher.wkang.receive.BatteryReceiver;
import com.launcher.wkang.util.NetUtils;
import com.launcher.wkang.util.Url;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class Fragment2 extends Fragment {
    private View view;
    private ListView morningList0;
    private ListView morningList1;
    private ListView morningList2;
    private ListView morningList3;
    private ListView morningList4;
    private ListView morningList5;
    private ListView morningList6;
    private ListView morningList7;

    private CourseTimeAdapter morningAdapter1;
    private CourseTimeAdapter morningAdapter2;
    private CourseTimeAdapter morningAdapter3;
    private CourseTimeAdapter morningAdapter4;
    private CourseTimeAdapter morningAdapter5;
    private CourseTimeAdapter morningAdapter6;
    private CourseTimeAdapter morningAdapter7;



    private ListView afternoonList0;
    private ListView afternoonList1;
    private ListView afternoonList2;
    private ListView afternoonList3;
    private ListView afternoonList4;
    private ListView afternoonList5;
    private ListView afternoonList6;
    private ListView afternoonList7;

    private CourseTimeAdapter afternoonAdapter1;
    private CourseTimeAdapter afternoonAdapter2;
    private CourseTimeAdapter afternoonAdapter3;
    private CourseTimeAdapter afternoonAdapter4;
    private CourseTimeAdapter afternoonAdapter5;
    private CourseTimeAdapter afternoonAdapter6;
    private CourseTimeAdapter afternoonAdapter7;
    private MorningTimeAdapter morningTimeAdapter;
    private AfterTimeAdapter afternoonTimeAdapter;
    private TextView title;
    private CourseReceiver courseReceiver;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view =getActivity().getLayoutInflater().inflate(R.layout.fragment_layout2,null);
        initView();
        initDate();
        return view;
    }
    private void initView(){
        morningList0=view.findViewById(R.id.morning_list0);
        morningList1=view.findViewById(R.id.morning_list1);
        morningList2=view.findViewById(R.id.morning_list2);
        morningList3=view.findViewById(R.id.morning_list3);
        morningList4=view.findViewById(R.id.morning_list4);
        morningList5=view.findViewById(R.id.morning_list5);
        morningList6=view.findViewById(R.id.morning_list6);
        morningList7=view.findViewById(R.id.morning_list7);

        afternoonList0=view.findViewById(R.id.afternoon_list0);
        afternoonList1=view.findViewById(R.id.afternoon_list1);
        afternoonList2=view.findViewById(R.id.afternoon_list2);
        afternoonList3=view.findViewById(R.id.afternoon_list3);
        afternoonList4=view.findViewById(R.id.afternoon_list4);
        afternoonList5=view.findViewById(R.id.afternoon_list5);
        afternoonList6=view.findViewById(R.id.afternoon_list6);
        afternoonList7=view.findViewById(R.id.afternoon_list7);

    }
    private Callback couListCallBack=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("Fragment2","IOException="+e.getMessage());

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("Fragment2","body="+response.body());
            String body=response.body().string();
            Log.d("Fragment2","couList="+body);
//            Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, null, null, null);
            if (null!=getActivity()) {
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("couList", body);
                editor.commit();
                resolveData(body);
            }


        }
    };
    private void initDate(){
        registerCourseReceiver();
        morningTimeAdapter=new MorningTimeAdapter(getActivity());
        morningList0.setAdapter(morningTimeAdapter);
        afternoonTimeAdapter=new AfterTimeAdapter(getActivity());
        afternoonList0.setAdapter(afternoonTimeAdapter);
        morningAdapter1=new CourseTimeAdapter(getActivity());
        morningList1.setAdapter(morningAdapter1);

        morningAdapter2=new CourseTimeAdapter(getActivity());
        morningList2.setAdapter(morningAdapter2);
        morningAdapter3=new CourseTimeAdapter(getActivity());
        morningList3.setAdapter(morningAdapter3);
        morningAdapter4=new CourseTimeAdapter(getActivity());
        morningList4.setAdapter(morningAdapter4);
        morningAdapter5=new CourseTimeAdapter(getActivity());
        morningList5.setAdapter(morningAdapter5);

        morningAdapter6=new CourseTimeAdapter(getActivity());
        morningList6.setAdapter(morningAdapter6);

        morningAdapter7=new CourseTimeAdapter(getActivity());
        morningList7.setAdapter(morningAdapter7);


//
        afternoonAdapter1=new CourseTimeAdapter(getActivity());
        afternoonList1.setAdapter(afternoonAdapter1);
        afternoonAdapter2=new CourseTimeAdapter(getActivity());
        afternoonList2.setAdapter(afternoonAdapter2);
        afternoonAdapter3=new CourseTimeAdapter(getActivity());
        afternoonList3.setAdapter(afternoonAdapter3);
        afternoonAdapter4=new CourseTimeAdapter(getActivity());
        afternoonList4.setAdapter(afternoonAdapter4);
        afternoonAdapter5=new CourseTimeAdapter(getActivity());
        afternoonList5.setAdapter(afternoonAdapter5);
        afternoonAdapter6=new CourseTimeAdapter(getActivity());
        afternoonList6.setAdapter(afternoonAdapter6);
        afternoonAdapter7=new CourseTimeAdapter(getActivity());
        afternoonList7.setAdapter(afternoonAdapter7);
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
        String  couList = sharedPreferences.getString("couList", null);
        if (!TextUtils.isEmpty(couList)){
            resolveData(couList);
        }
    }

    private void  resolveData(String data){
        try {
            Log.d("Fragment2","couList="+data);
            Gson gson = new Gson();
            final CouListBean couListBean=gson.fromJson(data, CouListBean.class);
            if (null==couListBean||couListBean.getDateList()==null||couListBean.getList()==null||getActivity()==null||getActivity().isFinishing()){
                return;
            }
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(couListBean.getDateList().size()>4) {
                        morningTimeAdapter.setData(couListBean.getDateList().subList(0, 4));
                        afternoonTimeAdapter.setData(couListBean.getDateList().subList(4, couListBean.getDateList().size()));
                    }
                    if (couListBean.getList().size()>6) {
                        if (couListBean.getList().get(0).size()>4) {
                            morningAdapter1.setData(couListBean.getList().get(0).subList(0, 4));
                            afternoonAdapter1.setData(couListBean.getList().get(0).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(1).size()>4) {
                            morningAdapter2.setData(couListBean.getList().get(1).subList(0, 4));
                            afternoonAdapter2.setData(couListBean.getList().get(1).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(2).size()>4) {
                            morningAdapter3.setData(couListBean.getList().get(2).subList(0, 4));
                            afternoonAdapter3.setData(couListBean.getList().get(2).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(3).size()>4) {
                            morningAdapter4.setData(couListBean.getList().get(3).subList(0, 4));
                            afternoonAdapter4.setData(couListBean.getList().get(3).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(4).size()>4) {
                            morningAdapter5.setData(couListBean.getList().get(4).subList(0, 4));
                            afternoonAdapter5.setData(couListBean.getList().get(4).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(5).size()>1) {
                            morningAdapter6.setData(couListBean.getList().get(5).subList(0, 4));
                            afternoonAdapter6.setData(couListBean.getList().get(5).subList(4, couListBean.getDateList().size()));
                        }
                        if (couListBean.getList().get(6).size()>1) {
                            morningAdapter7.setData(couListBean.getList().get(6).subList(0, 4));
                            afternoonAdapter7.setData(couListBean.getList().get(6).subList(4, couListBean.getDateList().size()));
                        }
                    }
                }
            });
        }catch (Exception e){

        }
    }

    @Override
    public void onResume() {
        super.onResume();
        App.getINstance().setsetWaveformMode(5,1);
        getcouList();

    }
    @Override
    public void onPause() {
        super.onPause();
    }
    private void getcouList(){
        Map<String,String>map=new HashMap<String, String>();
        map.put("deviceSerial",App.getINstance().deviceId);
        NetUtils.post(Url.couList,map,couListCallBack);
        App.getINstance().setsetWaveformMode(5,1);
        NetUtils.post(Url.couList,map,couListCallBack);
    }
    private class CourseReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            getcouList();
        }
    }
    private void registerCourseReceiver(){
        IntentFilter filter = new IntentFilter("CourseReceiver");
//        IntentFilter prohibitFilter = new IntentFilter("ProhibitReceiver");
        courseReceiver = new CourseReceiver();
//        prohibitReceiver=new ProhibitReceiver();
//        registerReceiver(prohibitReceiver,prohibitFilter);
        getActivity().registerReceiver(courseReceiver, filter);
    }

    @Override
    public void onDestroy() {
        getActivity().unregisterReceiver(courseReceiver);
        super.onDestroy();

    }
}
